/**
 * User: yeqingzhou01
 * Date: 13-12-30
 * Time: 下午6:53
 */
var error = require('../error/errors');
var util = require('util');
var dateUtils = require('./date-utils');
var throwRuleExceptionMsg = function (titleStr, contentStr) {
    var errMsg = '[' + titleStr + '] ';
    errMsg += (' ' + contentStr);
    throw error.getErrorInstance(
        error.errorsName.rule,
        errMsg
    );
}
var checkParamsExistence = function (paramsObj, checkTitle, requiredParamsArr) {
    var generateExceptionMsg = function (lostParams) {
        if (lostParams == undefined) {
            return;
        }
        var errMsg = '';
        for (var oneParamIdx in lostParams) {
            errMsg += (' ' + lostParams[oneParamIdx]);
        }
        throwRuleExceptionMsg(
            checkTitle,
            errMsg
        );
    }
    var main = function () {
        var lostParams = undefined;
        for (var oneParamIdx in requiredParamsArr) {
            if (!(requiredParamsArr[oneParamIdx] in paramsObj)) {
                if (lostParams == undefined) {
                    lostParams = [];
                }
                lostParams.push(requiredParamsArr[oneParamIdx]);
            }
        }
        generateExceptionMsg(lostParams);
    }
    main();
}
var throwTypeErrMsgWithErrParamsArr = function(titleStr, typeStr, illegalParams){
    if(illegalParams.length > 0){
        var errArgs = '';
        for(var paramNameIdx in illegalParams){
            errArgs += illegalParams[paramNameIdx];
        }
        throwRuleExceptionMsg(titleStr, 'Parameters [ ' + errArgs + ' ] should be [ ' + typeStr + ' ]');
    }
}
var checkParamsType = function (paramsObj, expTypeStr, paramsNameArr) {
    exports.checkParams('required', paramsObj, paramsNameArr);
    var invalidParams = [];
    for (var oneIdx in paramsNameArr) {
        if (typeof(paramsObj[paramsNameArr[oneIdx]]) !== expTypeStr) {
            invalidParams.push(paramsNameArr[oneIdx]);
        }
    }
//    if (invalidParams.length > 0) {
//        var paramsStr = ''
//        for (var oneParamNameIdx in invalidParams) {
//            paramsStr += invalidParams[oneParamNameIdx];
//            if (oneParamNameIdx != invalidParams.length - 1) {
//                paramsStr += ', ';
//            }
//        }
//        throwRuleExceptionMsg('PARAMS TYPE ERROR', 'Parameters [ ' + paramsStr + ' ] should be [ ' + expTypeStr + ' ]');
//    }
    throwTypeErrMsgWithErrParamsArr('PARAMS TYPE ERROR', expTypeStr, invalidParams);
}
exports.checkParams = function (func, paramsObj, paramsNameArr) {
    var funcs = {
        required: function (paramsObj, paramsNameArr) {
            checkParamsExistence(paramsObj, 'LOST REAUIRED PARAMS', paramsNameArr);
        },
        number: function (paramsObj, numParamsNameArr) {
            for (var oneParamIdx in numParamsNameArr) {
                if (
                    paramsObj[numParamsNameArr[oneParamIdx]] != undefined
                        && /^\d+$/.test(paramsObj[numParamsNameArr[oneParamIdx]])
                    ) {
                    try {
                        paramsObj[numParamsNameArr[oneParamIdx]] = parseInt(paramsObj[numParamsNameArr[oneParamIdx]]);
                    } catch (e) {
                    }
                }
            }
            checkParamsType(paramsObj, 'number', numParamsNameArr);
        },
        date: function (paramsObj, pageParamsNameArr) {
            var illegalParams = [];
            for (var paramIdx in pageParamsNameArr) {
                if(!dateUtils.AniDate.isDate(paramsObj[pageParamsNameArr[paramIdx]])){
                    illegalParams.push(pageParamsNameArr[paramIdx]);
                }
            }
            throwTypeErrMsgWithErrParamsArr('TIME ARG ERROR', 'DATE STRING', illegalParams);
        },
        datePeriod: function (paramsObj, pageParamsNameArr) {
            var illegalParams = [];
            for (var paramIdx in pageParamsNameArr) {
                var curDateStrArr = paramsObj[pageParamsNameArr[paramIdx]].split(',')
                for(var oneDateStrIdx in curDateStrArr){
                    if(!dateUtils.AniDate.isDate(curDateStrArr[oneDateStrIdx])){
                        illegalParams.push(pageParamsNameArr[paramIdx]);
                        break;
                    }
                }
            }
            throwTypeErrMsgWithErrParamsArr('TIME ARG ERROR', 'DATE STRING', illegalParams);
        },
        page: function (paramsObj, pageParamsNameArr) {
            exports.checkParams('required', paramsObj, ['curPage', 'perPage']);
            exports.checkParams('number', paramsObj, pageParamsNameArr);
        }
    }
    funcs[func](paramsObj, paramsNameArr);
}